<template>
  <div>
    <div>
      <el-input v-model="search" class="input-with-select" placeholder="请输入内容" style="width: 20%"
                @keyup.enter.native="searchClick">
        <el-button slot="append" icon="el-icon-search" @click="searchClick"></el-button>
      </el-input>
      <el-button
        :disabled="debuilds.length === 0"
        icon="el-icon-delete"
        style="float:left;"
        type="danger"
        @click="alldeBuild">
        一键删除
      </el-button>
    </div>
    <el-table
      v-loading="loading"
      :data="builds.records"
      border
      element-loading-background="rgba(0, 0, 0, 0.8)"
      @selection-change="changeBox"
      element-loading-spinner="el-icon-loading"
      element-loading-text="拼命加载中"
      style="width: 100%">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        align="center"
        label="Id"
        prop="buildId">
      </el-table-column>
      =
      <el-table-column
        align="center"
        label="楼栋">
        <template slot-scope="scope">
          <el-popover placement="top" trigger="hover" >
            <el-descriptions v-for="(user,index) in scope.row.userList" :key="index" :title="user.username" border
                             small="mini" >
              <el-descriptions-item label="手机号">
                <el-tag size="small"> {{ user.userPhone }}</el-tag>
              </el-descriptions-item>
              <el-descriptions-item label="性别">
                <el-tag size="small"> {{ user.userSex }}</el-tag>
              </el-descriptions-item>
            </el-descriptions>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.buildName }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="楼栋类型"
        :formatter="ifType"
        prop="buildType">
      </el-table-column>
      <el-table-column
        align="center"
        label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button type="primary" @click="addCleandialog">添加楼栋</el-button>
    <!--      添加页面-->
    <el-dialog
      :visible.sync="addVisible"
      title="添加楼栋"
      width="450px">
      <el-form :model="build"
               label-position="top"
               label-width="50px"
               size="min"
      >
        <el-form-item label="楼栋名称">
          <el-input v-model="build.buildName" autocomplete="true"></el-input>
        </el-form-item>
        <el-form-item label="宿舍类型">
          <el-select v-model="build.buildType" placeholder="宿舍类型">
            <el-option label="学生宿舍" value="1"></el-option>
            <el-option label="教师宿舍" value="2"></el-option>
            <el-option label="员工宿舍" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="addVisible = false">取消</el-button>
          <el-button type="primary" @click="addFrom">添加</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <div class="block">
      <el-pagination
        :page-size="limit"
        :total="total"
        background
        hide-on-single-page
        layout="prev, pager, next"
        @prev-click="prevClick"
        @next-click="nextClick"
        @current-change="handleCurrentChange">
      </el-pagination>
      <el-dialog
        :visible.sync="dialogVisible"
        title="编辑页面"
        width="30%">
        <el-form :model="build"
                 label-position="left"
                 label-width="auto">
          <el-form-item label="楼栋名">
            <el-input v-model="build.buildName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="楼栋类型">
            <el-select v-model="build.buildType">
              <el-option label="学生宿舍" value="1"></el-option>
              <el-option label="教师宿舍" value="2"></el-option>
              <el-option label="员工宿舍" value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="exitEdbuild">取消</el-button>
            <el-button type="primary" @click="editSubmit">修改</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import build from '@/utils/build'

export default {
  name: 'Build',
  data () {
    return {
      ids: [],
      debuilds: [],
      builds: [],
      search: '',
      total: 0,
      addVisible: false,
      loading: true,
      limit: 10,
      build: {
        buildName: '',
        buildType: '',
        buildId: '',
        userList: []
      },
      users: [],
      dialogVisible: false
    }
  },
  methods: {
    ifType (row, column, cellValue) {
      if (cellValue === '1') {
        return '学生宿舍'
      } else if (cellValue === '2') {
        return '教师宿舍'
      } else {
        return '员工宿舍'
      }
    },
    addCleandialog () {
      this.build = {}
      this.addVisible = true
    },
    addFrom () {
      build.addBuild(this.build).then(() => {
        this.getBuilds(this.builds.current, this.limit, this.search)
        this.$message.success('添加成功!')
        this.addVisible = false
        this.build = {}
      })
    },
    alldeBuild () {
      this.ids = []
      this.debuilds.forEach(builde => {
        this.ids.push(builde.buildId)
      })
      build.deleteAllBuild(this.ids).then(res => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getBuilds(this.builds.current, this.limit, this.search)
      })
    },
    changeBox (val) {
      this.debuilds = val
    },
    exitEdbuild () {
      this.dialogVisible = false
      this.build = {}
    },
    handleEdit (index, row) {
      Object.assign(this.build, row)
      this.dialogVisible = true
    },
    editSubmit () {
      console.log(this.build)
      build.editBuild(this.build).then(() => {
        this.getBuilds(this.builds.current, this.limit, this.search)
        this.$message.success('修改成功!')
        this.dialogVisible = false
        this.build = {}
      })
    },
    searchClick () {
      this.getBuilds(1, this.limit, this.search)
    },

    handleDelete (index, row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        build.deleteBuild(row.buildId).then(res => {
          this.getBuilds(this.builds.current, this.limit, this.search)
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleCurrentChange (page) {
      this.getBuilds(page, this.limit, this.search)
    },
    prevClick () {
      this.getBuilds(this.builds.current - 1, this.limit, this.search)
    },
    nextClick () {
      this.getBuilds(this.builds.current + 1, this.limit, this.search)
    },
    getBuilds (page, limit, search) {
      build.getBuilds(page, limit, search).then(res => {
        this.builds = res.data
        this.total = parseInt(this.builds.total)
        this.loading = false
      })
    }
  },
  created () {
    this.getBuilds(0, this.limit, this.search)
  }
}
</script>

<style scoped>

</style>
